﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Data;
using KTXD.Entities;
using System.Data;
using System.Threading;

namespace UserKTXD.Controls
{
    public partial class ControlTabThongBao : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


            }
            Pager1.CurrentIndex = 1;
            Pager2.CurrentIndex = 1;
            Pager3.CurrentIndex = 1;
            loadDataSV(Pager1.CurrentIndex);
            loadDataGV(Pager2.CurrentIndex);
            loadDataHD(Pager3.CurrentIndex);
        }

        //-------------------------------Load data Thông báo SV-------------------------------
        private void loadDataSV(int PageNo)
        {
            int TotalRows = 0;
            TList<HomeControlCapHai> TList_HomeControlCapHai = DataRepository.HomeControlCapHaiProvider.GetPaged("Id=2", "Id", 0, 0, out TotalRows);
            if (TList_HomeControlCapHai.Count > 0)
            {
                SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(TList_HomeControlCapHai[0].IdSubChuyenMuc);
                linkViewAll1.NavigateUrl = "~/SubChuyenMuc/" + GetTieuDe(ent_SubChuyenMuc.TenSubChuyenMuc) + "_" + ent_SubChuyenMuc.Id + ".aspx";
                string WhereClause = "IdSubChuyenMuc=" + TList_HomeControlCapHai[0].IdSubChuyenMuc + " AND TrangThai=1";
                TList<ChuyenMucCapHai> TList_Menu = DataRepository.ChuyenMucCapHaiProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
                drpThongBaoSV.DataSource = TList_Menu;
                drpThongBaoSV.DataBind();
                Pager1.ItemCount = TotalRows;
                H_Page1Count.Value = Pager1.PageCount.ToString();
            }
        }

        //-------------------------------Load data Thông báo GV-------------------------------
        private void loadDataGV(int PageNo)
        {
            int TotalRows = 0;
            TList<HomeControlCapHai> TList_HomeControlCapHai = DataRepository.HomeControlCapHaiProvider.GetPaged("Id=1", "Id", 0, 0, out TotalRows);
            if (TList_HomeControlCapHai.Count > 0)
            {
                SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(TList_HomeControlCapHai[0].IdSubChuyenMuc);
                linkViewAll2.NavigateUrl = "~/SubChuyenMuc/" + GetTieuDe(ent_SubChuyenMuc.TenSubChuyenMuc) + "_" + ent_SubChuyenMuc.Id + ".aspx";
                string WhereClause = "IdSubChuyenMuc=" + TList_HomeControlCapHai[0].IdSubChuyenMuc + " AND TrangThai=1";
                TList<ChuyenMucCapHai> TList_Menu = DataRepository.ChuyenMucCapHaiProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager2.PageSize, out TotalRows);
                drpThongBaoGV.DataSource = TList_Menu;
                drpThongBaoGV.DataBind();
                Pager2.ItemCount = TotalRows;
                H_Page2Count.Value = Pager2.PageCount.ToString();
            }
        }

        //-------------------------------Load data Thông báo HD-------------------------------
        private void loadDataHD(int PageNo)
        {
            int TotalRows = 0;
            TList<HomeControlCapHai> TList_HomeControlCapHai = DataRepository.HomeControlCapHaiProvider.GetPaged("Id=4", "Id", 0, 0, out TotalRows);
            if (TList_HomeControlCapHai.Count > 0)
            {
                SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(TList_HomeControlCapHai[0].IdSubChuyenMuc);
                linkViewAll3.NavigateUrl = "~/SubChuyenMuc/" + GetTieuDe(ent_SubChuyenMuc.TenSubChuyenMuc) + "_" + ent_SubChuyenMuc.Id + ".aspx";
                string WhereClause = "IdSubChuyenMuc=" + TList_HomeControlCapHai[0].IdSubChuyenMuc + " AND TrangThai=1";
                TList<ChuyenMucCapHai> TList_Menu = DataRepository.ChuyenMucCapHaiProvider.GetPaged(WhereClause, "Id DESC", PageNo - 1, Pager3.PageSize, out TotalRows);
                drpHoatDong.DataSource = TList_Menu;
                drpHoatDong.DataBind();
                Pager3.ItemCount = TotalRows;
                H_Page3Count.Value = Pager3.PageCount.ToString();
            }
        }

        //-------------------------------Lấy tiêu đề-------------------------------
        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }

        //-------------------------------Xử lý Phân Trang Tab 1-------------------------------
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        loadDataSV(Pager1.CurrentIndex);
    }

    protected void cmdNext1_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex < Convert.ToInt32(H_Page1Count.Value))
        {
            Thread.Sleep(2000);
            Pager1.CurrentIndex = Pager1.CurrentIndex + 1;
            loadDataSV(Pager1.CurrentIndex);
        }
    }

    protected void cmdPrevious1_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex > 1)
        {
            Thread.Sleep(2000);
            Pager1.CurrentIndex = Pager1.CurrentIndex - 1;
            loadDataSV(Pager1.CurrentIndex);
        }
    }
        //--------------------------Kết thúc Tab2--------------------------


        //-------------------------------Xử lý Phân Trang Tab 2-------------------------------
        protected void Pager2_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager2.CurrentIndex = currnetPageIndex;
            loadDataGV(Pager2.CurrentIndex);
        }

        protected void cmdNext2_Click(object sender, ImageClickEventArgs e)
        {
            if (Pager2.CurrentIndex < Convert.ToInt32(H_Page2Count.Value))
            {
                Thread.Sleep(2000);
                Pager2.CurrentIndex = Pager2.CurrentIndex + 1;
                loadDataGV(Pager2.CurrentIndex);
            }
        }

        protected void cmdPrevious2_Click(object sender, ImageClickEventArgs e)
        {
            if (Pager2.CurrentIndex > 1)
            {
                Thread.Sleep(2000);
                Pager2.CurrentIndex = Pager2.CurrentIndex - 1;
                loadDataGV(Pager2.CurrentIndex);
            }
        }
        //--------------------------Kết thúc Tab2--------------------------


        //-------------------------------Xử lý Phân Trang Tab 3-------------------------------
        protected void Pager3_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager3.CurrentIndex = currnetPageIndex;
            loadDataHD(Pager3.CurrentIndex);
        }

        protected void cmdNext3_Click(object sender, ImageClickEventArgs e)
        {
            if (Pager3.CurrentIndex < Convert.ToInt32(H_Page3Count.Value))
            {
                Thread.Sleep(2000);
                Pager3.CurrentIndex = Pager3.CurrentIndex + 1;
                loadDataHD(Pager3.CurrentIndex);
            }
        }

        protected void cmdPrevious3_Click(object sender, ImageClickEventArgs e)
        {
            if (Pager3.CurrentIndex > 1)
            {
                Thread.Sleep(2000);
                Pager3.CurrentIndex = Pager3.CurrentIndex - 1;
                loadDataHD(Pager3.CurrentIndex);
            }
        }
        //--------------------------Kết thúc Tab3--------------------------3
    }
}